﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WebApplication3.Data;
using WebApplication3.Data.Entities;
using WebApplication3.Infrastructure.Data;
using WebApplication3.Interfaces;

namespace WebApplication3
{
	public class Startup
	{
		public Startup(IConfiguration configuration)
		{
			Configuration = configuration;

			//从配置文件读取
			ConstData.DevId = configuration.GetValue<int>("DevId");
			ConstData.DevKey = configuration.GetValue<string>("DevKey");
			ConstData.ShopId = configuration.GetValue<string>("ShopId");
			ConstData.ApiBaseUrl = configuration.GetValue<string>("ApiBaseUrl");

			//从配置文件读取
			ConstData.WXAppId = configuration.GetValue<string>("WX:AppId");
			ConstData.WXAppSecret = configuration.GetValue<string>("WX:AppSecret");
			ConstData.WXJsCode = configuration.GetValue<string>("WX:JsCode");

			//bmobconfig从配置文件读取
			ConstData.bmobConfig.ApplicationID = configuration.GetValue<string>("bmob:ApplicationID");
			ConstData.bmobConfig.RESTAPIKey = configuration.GetValue<string>("bmob:RESTAPIKey");
			ConstData.bmobConfig.SecretKey = configuration.GetValue<string>("bmob:SecretKey");
		}

		public IConfiguration Configuration { get; }

		// This method gets called by the runtime. Use this method to add services to the container.
		public void ConfigureServices(IServiceCollection services)
		{
			services.Configure<CookiePolicyOptions>(options =>
			{
				// This lambda determines whether user consent for non-essential cookies is needed for a given request.
				options.CheckConsentNeeded = context => true;
				options.MinimumSameSitePolicy = SameSiteMode.None;
			});
			services.AddDbContext<ApplicationDbContext>(c =>
			   c.UseSqlServer(Configuration.GetConnectionString("MyDataContex")));

			services.AddIdentityCore<AppIdentityUser>()
				.AddRoles<AppIdentityRole>() 
				.AddEntityFrameworkStores<ApplicationDbContext>()
				.AddSignInManager()
				.AddDefaultTokenProviders();

			 
			services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
			 .AddCookie(options =>
			 {
				 //options.LoginPath = new PathString("/Account/login");
				 //options.AccessDeniedPath = new PathString("/Account/AccessDenied");
			 });


			services.AddCap(x =>
			{
				//如果你使用的 EF 进行数据操作，你需要添加如下配置：
				x.UseEntityFramework<ApplicationDbContext>();  //可选项，你不需要再次配置 x.UseSqlServer 了
				 

				//如果你使用的 RabbitMQ 或者 Kafka 作为MQ，根据使用选择配置：
				x.UseRabbitMQ("localhost"); 
			});


			services.AddAuthentication(o =>
			{
				o.DefaultScheme = IdentityConstants.ApplicationScheme;
				o.DefaultSignInScheme = IdentityConstants.ExternalScheme;
			
			})
			.AddIdentityCookies(o => { });

		 


			services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

			 
			services.AddTransient(typeof(IRepository<>), typeof(EfRepository<>));
			services.AddTransient(typeof( IAsyncRepository<>) ,typeof( EfRepository<>));
			services.AddTransient<Services.SFOrderService>();


		}

		// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
		public void Configure(IApplicationBuilder app, IHostingEnvironment env)
		{
			if (env.IsDevelopment())
			{
				app.UseDeveloperExceptionPage();
			}
			else
			{
				app.UseHsts();
			}

			//app.UseHttpsRedirection();
			app.UseStaticFiles();
			app.UseCookiePolicy();

			app.UseAuthentication();

			app.UseMvc(routes =>
			{
				routes.MapRoute(
					name: "default",
					template: "{controller=Home}/{action=Index}/{id?}");
			});
		}
	}
}
